package tech.heptagram.staff.api.v3;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import tech.heptagram.core.common.rest.ResponseResult;
import tech.heptagram.staff.model.Department;
import tech.heptagram.staff.query.DepartmentQuery;
import tech.heptagram.staff.query.DepartmentRemoveReq;
import tech.heptagram.staff.query.DepartmentReq;
import tech.heptagram.staff.service.DepartmentService;

/**
 * @author wang.yuchuan
 * @date 2018/9/10
 */
@Api("部门相关api")
@RestController
@RequestMapping("/api/v3/department")
@Slf4j
public class DepartmentApiRestController implements DepartmentApi {

    @Resource
    private DepartmentService departmentService;

    @ApiOperation("根据部门id获取部门信息")
    @GetMapping("/get/{id}")
    @Override
    public ResponseResult<Department> getById(@PathVariable Integer id) {
        log.info("根据部门id获取部门信息 getById 开始 接受参数:id {} ", id);
        Department department = departmentService.getById(id);
        log.info("根据部门id获取部门信息 getById 结束 返回结果 {} ", department);
        return new ResponseResult<>(department);
    }

    @ApiOperation("根据父部门id获取部门信息")
    @GetMapping("/get/parentId/{parentId}")
    @Override
    public ResponseResult<List<Department>> getByParentId(@PathVariable Integer parentId) {
        log.info("根据父部门id获取部门信息 getById 开始 接受参数:id {} ", parentId);
        List<Department> departments = departmentService.getByParentId(parentId);
        log.info("根据父部门id获取部门信息 getById 结束 返回结果 {} ", departments);
        return new ResponseResult<>(departments);
    }

    @ApiOperation("根据查询条件获取部门信息")
    @PostMapping("/find")
    @Override
    public ResponseResult<List<Department>> findByQuery(@RequestBody DepartmentQuery departmentQuery) {
        log.info("根据部门ids获取部门信息 findByQuery 开始 接受参数:departmentQuery {} ", departmentQuery);
        List<Department> departments = departmentService.findByQuery(departmentQuery);
        log.info("根据部门ids获取部门信息 findByQuery 结束 返回结果 {} ", departments);
        return new ResponseResult<>(departments);
    }

    @ApiOperation("根据员工编码获取部门信息")
    @GetMapping("/get/staff/{staffCode}")
    @Override
    public ResponseResult<List<Department>> findByStaffCode(@PathVariable String staffCode) {
        log.info("根据staffCode获取部门信息 findByStaffCode 开始 接受参数:staffCode {} ", staffCode);
        List<Department> departments = departmentService.findByStaffCode(staffCode);
        log.info("根据staffCode获取部门信息 findByStaffCode 结束 返回结果 {} ", departments);
        return new ResponseResult<>(departments);
    }

    @ApiOperation("根据钉钉部门id获取部门信息")
    @GetMapping("/get/dingtalk/{dingDepartmentId}")
    @Override
    public ResponseResult<Department> findByDingDepartmentId(@PathVariable("dingDepartmentId") Long dingDepartmentId) {
        log.info("根据钉钉部门id获取部门信息 开始 接受参数:id {} ", dingDepartmentId);
        Department department = departmentService.findByDingDepartmentId(dingDepartmentId);
        log.info("根据钉钉部门id获取部门信息 结束 返回结果 {} ", department);
        return new ResponseResult<>(department);
    }

    @ApiOperation("新增部门信息")
    @PostMapping("/save")
    @Override
    public ResponseResult<Integer> save(@RequestBody DepartmentReq req) {
        log.info("添加新部门 接收参数：{}",req);
        Integer deptId = departmentService.save(req);
        log.info("添加新部门 返回的部门ID为：{}",deptId);
        return new ResponseResult<>(deptId);
    }

    @ApiOperation("修改部门信息")
    @PostMapping("/update")
    @Override
    public ResponseResult modify(@RequestBody DepartmentReq departmentReq) {
        log.info("修改部门信息 接收参数：{}",departmentReq);
        departmentService.update(departmentReq);
        log.info("修改部门信息 调用完成");
        return new ResponseResult<>();
    }

    @ApiOperation("删除部门信息")
    @PostMapping("/delete")
    @Override
    public ResponseResult delete(@RequestBody DepartmentRemoveReq departmentReq) {
        log.info("删除部门信息 接收参数：{}",departmentReq);
        departmentService.delete(departmentReq);
        log.info("删除部门信息 调用完成");
        return new ResponseResult();
    }
}
